<template>
  <div>
    <div>
      <el-menu
        :default-active="this.$route.path"
        mode="horizontal"
        :router="true"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b"
      >
        <el-menu-item>
          <img src="../assets/logo.png" alt="" height="24px" />
        </el-menu-item>
        <el-menu-item index="/customer/dashboard">仪表板</el-menu-item>
        <el-menu-item index="/customer/create">录入客户信息</el-menu-item>
        <el-menu-item index="/customer/list">查看客户信息</el-menu-item>
        <el-menu-item index="/user/manage" v-if="this.$store.state.currentUser.role === 'USERMANAGER'">用户管理</el-menu-item>
        <el-submenu style="float: right" index="">
          <template slot="title"
            >欢迎您 [{{ paramsForUpdate.username }}]</template
          >
          <el-menu-item @click="openDialog">修改密码</el-menu-item>
          <el-menu-item index="/" @click="handleLogout">注销</el-menu-item>
        </el-submenu>
      </el-menu>
    </div>
    <el-dialog title="" :visible.sync="dialogVisible" width="30%">
      <div>
        <el-form
          ref="form"
          :model="paramsForUpdate"
          :rules="rules"
          label-width="100px"
          size="medium"
        >
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input
              v-model="paramsForUpdate.oldPassword"
              style="width: 60%"
              type="password"
              placeholder="请输入旧密码"
            ></el-input>
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input
              v-model="paramsForUpdate.newPassword"
              style="width: 60%"
              type="password"
              placeholder="请输入新密码"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认新密码" prop="checkNewPassword">
            <el-input
              v-model="paramsForUpdate.checkNewPassword"
              style="width: 60%"
              type="password"
              placeholder="请再次输入新密码"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-success"
              :loading="loading"
              @click="updatePassword"
              >{{ loading ? "提交中 ..." : "保存" }}</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <el-main>
      <router-view></router-view>
    </el-main>
  </div>
</template>

<script>
export default {
  data() {
    var validatePass1 = (rule, value, callback) => {
      if (!value || value != this.paramsForUpdate.oldPassword) {
        return callback("请输入正确密码");
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (!value || value != this.paramsForUpdate.newPassword) {
        return callback("请输入正确密码");
      }
    };
    return {
      dashboard: "/customer/dashboard",
      paramsForUpdate: {
        username: "",
        oldPassword: "",
        newPassword: "",
        checkNewPassword: "",
      },
      dialogVisible: false,
      rules: {
        oldPassword: [{ validator: validatePass1, trigger: "blur" }],
        newPassword: [
          { required: true, message: "请输入新密码", trigger: "blur" },
        ],
        checkNewPassword: [{ validator: validatePass2, trigger: "blur" }],
      },
      loading: false,
    };
  },
  created() {
    this.paramsForUpdate.username = this.$store.state.currentUser.username;
    this.paramsForUpdate.oldPassword = this.$store.state.currentUser.password;
  },
  methods: {
    handleLogout() {
      sessionStorage.clear();
    },
    openDialog() {
      this.dialogVisible = true;
    },
    updatePassword() {
      if (this.loading) {
        return;
      }
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          setTimeout(() => {
            this.axios
              .put("/user/updatePassword", this.paramsForUpdate)
              .then(() => {
                this.$message.success("密码更新成功");
                this.loading = false;
              })
              .catch(() => {
                this.$message.error("密码更新失败");
                this.loading = false;
              });
          }, 3000);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "取消更新密码",
          });
        });
    },
  },
};
</script>

<style scoped>
nav {
  margin-bottom: 40px;
}
</style>